package com.example.eeuse.util;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.example.eeuse.listener.BankAccountVoListenter;
import com.example.eeuse.listener.SubsidyMesDataVoListenter;
import com.example.eeuse.mapper.SubsidyRecordDataMapper;
import com.example.eeuse.mapper.BankAccountMapper;
import com.example.eeuse.model.SubsidyRecordData;
import com.example.eeuse.model.vo.*;
import lombok.extern.slf4j.Slf4j;
 import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

@Slf4j
@Component
public class SimpleSchedule {

    @Resource
    private SubsidyRecordDataMapper subsidyRecordDataMapper;

    @Resource
    BankAccountMapper bankAccountMapper;

    @Autowired
    RestHighLevelClient levelClient;

    /**
     * 定时器定义，设置执行时间
     */
    //@Scheduled(cron = "*/5 * * * * ?")
    private void readProcess() {

        log.info(JSON.toJSONString(levelClient.indices()) +"----ES index-----");


//        log.info("操作系统：{}"+OshiUtil.getOs());
//
//      log.info("操作system系统：{}"+OshiUtil.getSystem());

        /**
         * 查询数据库读取文件地址
         *
         */
        String  path="E:\\xls";
        try (Stream<Path> paths = Files.walk(Paths.get(path))){
            paths .filter(Files::isRegularFile)
                    .forEach(file ->{
                        log.info(file.toFile().getPath());
                        //log.info(file.toFile().getAbsolutePath());
                        EasyExcel.read(file.toFile(), SubsidyRecordDataVo.class, new SubsidyMesDataVoListenter(subsidyRecordDataMapper)).sheet(0).doRead();
                    });

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //@Scheduled(cron = "*/5 * * * * ?")
    private void  autoProcess() {
        List<SubsidyRecordData> list=new ArrayList<>();
        String code= DateUtil.now();
        String bentchId = IdUtil.fastUUID();

        for (int i = 0; i <5000 ; i++) {
            String name=AutoNameUtil.autoName();
            SubsidyRecordData build = SubsidyRecordData.builder()
                    .id(IdUtil.fastUUID())
                    .collectId(IdUtil.fastUUID())
                    .peopleId(RandomUtil.randomNumbers(19))
                    .peopleVersion("2")
                    .accountVersoin("2")
                    .projectId(RandomUtil.randomString(1).toUpperCase() + "-" + RandomUtil.randomString(2).toUpperCase() + RandomUtil.randomNumbers(3))
                    .regionId(RandomUtil.randomString(16))
                    .yearId(2022)
                    .batchId(bentchId)
                    .money(RandomUtil.randomLong(65532,865454)/20F)
                    .operatorId("410211013")
                    .operatorTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .status("6")
                    .assingTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .payTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .assingId(IdUtil.fastUUID())
                    .bankType("02")
                    .accountCode(RandomUtil.randomNumbers(18))
                    .accountName(name)
                    .name(name)
                    .createTime(new Date())
                    .build();
            list.add(build);
        }
        Integer integer = subsidyRecordDataMapper.insertBatch(list);
        log.info("批量报错成果{},报错数量为：{}",code,integer);
     }


    /****
     * 读取文件解析入库 5 * * * * ?
     */
    //@Scheduled(cron = "* * * * * ? *")
    private void  bankReslutData() {
        String path="E:\\xls\\01.xlsx";
        String bentchId = UUID.randomUUID().toString();
        log.info("导入文件{},批次ID {}",path,bentchId);
        EasyExcel.read(path, SubsidyRecordDataVo.class, new BankAccountVoListenter(bankAccountMapper)).sheet(0).doRead();
        log.info("批次ID {} 导入完成",bentchId);
    }

    /**
     * 银行卡信息
     */
    private void  peopleBankData() {
        String path="file.toFile()";
        EasyExcel.read(path, BankAccountVo.class, new BankAccountVoListenter(bankAccountMapper)).sheet(0).doRead();

    }

}
